package com.zzl.bonds.ticket.controller;

import com.zzl.apidoc.annotation.ApidocController;
import com.zzl.apidoc.annotation.ApidocOperation;
import com.zzl.apidoc.annotation.ApidocParam;
import com.zzl.bonds.ticket.constant.status.HandStatusEnum;
import com.zzl.bonds.ticket.constant.type.ContractTicketTypeEnum;
import com.zzl.bonds.ticket.constant.type.TicketTypeEnum;
import com.zzl.bonds.ticket.dto.ApUserShowInfo;
import com.zzl.bonds.ticket.dto.KeyValue;
import com.zzl.bonds.ticket.dto.feign.ApprovalSubmitDto;
import com.zzl.bonds.ticket.dto.finance.*;
import com.zzl.bonds.ticket.dto.ticketopen.CheckSubmit;
import com.zzl.bonds.ticket.service.TicketFinanceService;
import com.zzl.general.common.apidoc.annotation.ApidocParamsPage;
import com.zzl.general.common.apidoc.annotation.ApidocResultKeyValR;
import com.zzl.general.common.apidoc.annotation.ApidocReturnPage;
import com.zzl.general.common.apidoc.annotation.ApidocReturnR;
import com.zzl.general.common.apidoc.constants.ResultTypeInR;
import com.zzl.general.common.ctrl.WebBaseCtrl;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.utils.LogUtils;
import com.zzl.general.common.utils.PageUtils;
import com.zzl.general.common.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 融资申请单
 *
 * @author wztsl
 * @date 2022-02-24 13:53:42
 */
@ApidocController("6融资申请单")
@RestController
@RequestMapping("bondsticket/ticketFinance")
@Slf4j
public class TicketFinanceController extends WebBaseCtrl {
    @SuppressWarnings("all")
    @Autowired
    private TicketFinanceService ticketFinanceService;

    @GetMapping("/test")
    public Result test() {
        try {
            ticketFinanceService.setBusinessProtocol("de85c6bf17d646ae4c22808ee12318550", ContractTicketTypeEnum.TICKET_TYPE_OPEN.getCode());
            return Result.ok();
        } catch (SysException e) {
            return Result.error(e.getMsg());
        }
    }

    /**
     * 获取处理状态
     */
    @GetMapping("/handStatus")
    @ApidocOperation(description = "1获取处理状态", operationId = "ticket/ticketfinance/handStatus")
    @ApidocReturnR
            (otherkv = {
                    @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_COLLOCTION, key = "list", genericTypeClass = KeyValue.class)
            })
    public Result approveStatus() {
        return Result.ok().put("list", HandStatusEnum.getKvList(TicketTypeEnum.TICKET_TYPE_FINANCE.getCode()));
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApidocOperation(description = "1融单填写保存", operationId = "ticket/ticketFinance/save")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "data", valueClass = String.class)
    })
    public Result save(@ApidocParam(name = "保存的融资申请单对象值", required = true) @RequestBody @Validated TicketFinanceAddDto ticketFinance) {
        try {
            String id = ticketFinanceService.saveTicketFinance(ticketFinance, this.getUser());
            return Result.ok().put("data", id);
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    @GetMapping("/getById/{id}")
    @ApidocOperation(description = "1融单填写页面信息查询", operationId = "/ticket/ticketfinance/getById123")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "data", valueClass = MyTicketDetailDto.class)
    })
    public Result getById(@ApidocParam(name = "id", description = "我的凭证id", required = true) @PathVariable("id") String id) {
        try {
            return Result.ok().put("data", ticketFinanceService.getTicketInfoById(id, this.getUser()));
        } catch (SysException e) {
            e.printStackTrace();
            return Result.error(e.getMsg());
        }
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApidocOperation(description = "1分页查询融资列表", operationId = "ticket/ticketfinance/list")
    @ApidocReturnPage(genericTypeClass = TicketFinanceListDto.class)
    public Result list(@ApidocParamsPage(name = "params",
            keyName = {
                    "ticketNo"
                    , "startAmount"
                    , "endAmount"
                    , "handStatus"
                    , "financeInstitutyId"
                    , "startTime"
                    , "endTime"
                    , "page"
                    , "order"
            }
            , keyDescription = {
            "凭证编号"
            , "开始金额"
            , "结束金额"
            , "处理状态，见接口与原型 依次 1,2,3,4,必传，默认全部 "
            , "金融机构id"
            , "申请日期开始时间"
            , "申请日期结束时间"
            , "排序类型：create_time（默认）、sell_amount：融资本金、estimated_financing_interest：预计融资利息，" +
            "service_chage：预计服务费用,create_time:申请日期，cash_time：到期日期"
            , "排序方式：desc 降序（默认）asc 升序"})
                       @RequestParam Map<String, Object> params) {
        try {
            PageUtils page = ticketFinanceService.queryPage(params, this.getUser());
            return Result.ok().put("page", page);
        } catch (SysException e) {
            e.printStackTrace();
            return Result.error(e.getMsg());
        }
    }


    @GetMapping("/financeData")
    @ApidocOperation(description = "1查看融资统计金额", operationId = "ticket/ticketfinance/financeData")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "data", valueClass = TicketFinanceDataDto.class)
    })
    public Result financeData() {
        try {
            return Result.ok().data(ticketFinanceService.financeData(this.getUser()));
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApidocOperation(description = "1融单详情", operationId = "/ticket/ticketfinance/info-test")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "data", valueClass = TicketFinanceDto.class)
    })
    public Result info(@ApidocParam(name = "id", description = "融资id", required = true) @PathVariable("id") String id) {
        try {
            return Result.ok().put("data", ticketFinanceService.getFinanceDetailById(id, this.getUser()));
        } catch (SysException e) {
            return Result.error(e.getMsg());
        }
    }

    @GetMapping("/getFinanceProcessProgress/{id}")
    @ApidocOperation(description = "1流程进度", operationId = "/ticket/getFinanceProcessProgress/info")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "list", valueClass = ApUserShowInfo.class)
    })
    public Result getFinanceProcessProgress(@ApidocParam(name = "id", description = "融单申请表id", required = true) @PathVariable("id") String id) {
        try {
            return Result.ok().put("list", ticketFinanceService.getFinanceProcessProgress(id));
        } catch (SysException e) {
            return Result.error(e.getMsg());
        }
    }

    @GetMapping("/reviewList")
    @ApidocOperation(description = "1融资复核列表", operationId = "ticket/ticketfinance/reviewList")
    @ApidocReturnPage(genericTypeClass = TicketFinanceListDto.class)
    public Result reviewList(@ApidocParamsPage(name = "params",
            keyName = {
                    "ticketNo"
                    , "startAmount"
                    , "endAmount"
                    , "financeInstitutyId"
                    , "page"
                    , "order"
            }
            , keyDescription = {
            "凭证编号"
            , "开始金额"
            , "结束金额"
            , "金融机构id "
            , "排序类型：create_time（默认）、sell_amount：融资本金、estimated_financing_interest：预计融资利息，" +
            "service_chage：预计服务费用,create_time:申请日期，cash_time：到期日期"
            , "排序方式：desc 降序（默认）asc 升序"})
                             @RequestParam Map<String, Object> params) {
        try {
            PageUtils page = ticketFinanceService.queryReviewFinancePage(params, this.getUser(), this.getSession());
            return Result.ok().put("page", page);
        } catch (SysException e) {
            e.printStackTrace();
            return Result.error(e.getMsg());
        }
    }

    /**
     * 原型字段修改，和融单详情复用一个接口
     */
    //    @GetMapping("/reviewInfo/{id}")
    @ApidocOperation(description = "融单复核详情", operationId = "/ticket/ticketfinance/reviewInfo")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "data", valueClass = TicketFinanceReviewDto.class)
    })
    public Result reviewInfo(@ApidocParam(name = "id", description = "融资id", required = true) @PathVariable("id") String id) {
        try {
            return Result.ok().put("data", ticketFinanceService.reviewInfo(id, this.getUser()));
        } catch (SysException e) {
            return Result.error(e.getMsg());
        }
    }

    @PostMapping("/financeTicketCheckSubmit")
    @ApidocOperation(description = "1融资复核提交", operationId = "/ticket/ticketfinance/financeTicketCheckSubmit")
    @ApidocReturnR
    public Result financeTicketCheckSubmit(@ApidocParam(name = "融资复核提交", required = true) @Validated @RequestBody ApprovalSubmitDto checkSubmit) {
        try {
            ticketFinanceService.financeTicketCheckSubmit(checkSubmit, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            return Result.error(e.getMsg());
        }
    }

    @GetMapping("/signList")
    @ApidocOperation(description = "融资协议签署列表", operationId = "ticket/ticketfinance/signList")
    @ApidocReturnPage(genericTypeClass = TicketFinanceListDto.class)
    public Result signList(@ApidocParamsPage(name = "params",
            keyName = {
                    "ticketNo"
                    , "startAmount"
                    , "endAmount"
                    , "financeInstitutyId"
                    , "page"
                    , "order"
            }
            , keyDescription = {
            "凭证编号"
            , "开始金额"
            , "结束金额"
            , "金融机构id "
            , "排序类型：create_time（默认）、sell_amount：融资本金、estimated_financing_interest：预计融资利息，" +
            "service_chage：预计服务费用,create_time:申请日期，cash_time：到期日期"
            , "排序方式：desc 降序（默认）asc 升序"})
                           @RequestParam Map<String, Object> params) {
        PageUtils page = ticketFinanceService.querySignFinancePage(params, this.getUser(), this.getSession());
        return Result.ok().put("page", page);
    }

    @PostMapping("/financeTicketSignSubmit/{id}")
    @ApidocOperation(description = "融资签署提交", operationId = "/ticket/ticketfinance/financeTicketSignSubmit1")
    @ApidocReturnR
    public Result financeTicketSignSubmit(@ApidocParam(name = "id", description = "融资id", required = true) @PathVariable("id") String id) {
        try {
            ticketFinanceService.financeTicketSignSubmit(id, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            return Result.error(e.getMsg());
        }
    }

    @GetMapping("/cancel/{id}")
    @ApidocOperation(description = "1撤销申请", operationId = "/ticket/ticketfinance/cancel")
    @ApidocReturnR
    public Result financeTicketCancel(@ApidocParam(name = "id", description = "融资id", required = true) @PathVariable("id") String id) {
        try {
            ticketFinanceService.financeTicketCancel(id, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            return Result.error(e.getMsg());
        }
    }
}
